package com.audaque.springboot.foshanupload.web.raspcloud.register;

import com.audaque.springboot.foshanupload.authcore.model.currentUser.TransmittableThreadLocalCurrentUser;
import com.audaque.springboot.foshanupload.event.event.LoginSuccessEvent;
import com.audaque.springboot.foshanupload.web.raspcloud.entity.User;
import com.audaque.springboot.foshanupload.web.raspcloud.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class LoginEventListener {
    @Autowired
    private UserMapper userMapper;
    @EventListener
    public void handleNotifyEvent(LoginSuccessEvent event) {
        TransmittableThreadLocalCurrentUser transmittableThreadLocalCurrentUser = event.getTransmittableThreadLocalCurrentUser();
        String id = transmittableThreadLocalCurrentUser.getId();
        String loginIp = transmittableThreadLocalCurrentUser.getLoginIp();
        User user = new User();
        user.setId(Long.parseLong(id));
        user.setLoginDate(LocalDateTime.now());
        user.setLoginIp(loginIp);
        userMapper.updateById(user);
    }

}

